package com.xxb.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.xxb.entity.Comment;
import com.xxb.entity.User;
import com.xxb.util.DBHelp;
import com.xxb.util.Rowmapper;

public class CommentDao {
	
	private DBHelp<Comment> db = new DBHelp<Comment>();
	
	public List<Comment> findBypid(int pid){
		String sql = "SELECT t_comment.id as cid,content,uid,pid,comtime ,useremail FROM t_comment LEFT JOIN t_user ON t_comment.uid = t_user.id where pid=? order by comtime desc";
		return db.quertyList(sql,new CommentDaoRowmapper(),pid);
		
	}
	
	public int save(Comment comment){
		String sql ="insert into t_comment(id ,content,uid,pid,comtime) values(?,?,?,?,?)";
		return db.executeSQL(sql, comment.getId(),comment.getContent(),comment.getUid(),comment.getPid(),comment.getComtime());
	}
	
	private class CommentDaoRowmapper implements Rowmapper<Comment>{

		public Comment mapRow(ResultSet rs) throws SQLException {
			Comment comment = new Comment();
			
			comment.setComtime(rs.getString("comtime"));
			comment.setContent(rs.getString("content"));
			comment.setId(rs.getInt("cid"));
			comment.setPid(rs.getInt("pid"));
			comment.setUid(rs.getInt("uid"));
			
			User user = new User();
			user.setUseremail(rs.getString("useremail"));
			
			comment.setUser(user);
			return comment;
		}
		
	}
}
